<?php session_start();
require_once('Connections/db.php'); 
require_once('funciones/funciones.php'); 

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
  $GLOBALS['PrevUrl'] = $accesscheck;
  session_register('PrevUrl');
}

if (isset($_POST['Usuario'])) {
  $loginUsername=$_POST['Usuario'];
  $password=Encriptar($_POST['Password']);
  $MM_fldUserAuthorization = "GRUP_CODIGO";
  $MM_redirectLoginSuccess = "admusuarios.php";
  $MM_redirectLoginFailed = "login.php?error=1";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_db, $db);
  	
  $LoginRS__query=sprintf("SELECT USUA_LOGIN, USUA_PASSWO, GRUP_CODIGO FROM usuarios WHERE USUA_LOGIN='%s' AND USUA_PASSWO='%s'",
  get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password)); 
   
  $LoginRS = mysql_query($LoginRS__query, $db) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
    
    $loginStrGroup  = mysql_result($LoginRS,0,'GRUP_CODIGO');
    
    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;	      

    //register the session variables
    session_register("MM_Username");
    session_register("MM_UserGroup");

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
  <p>&nbsp;  </p>
  <table width="20%"  border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Usuario: </td>
      <td><input name="Usuario" type="text" id="Usuario"></td>
    </tr>
    <tr>
      <td>Password: </td>
      <td><input name="Password" type="text" id="Password"></td>
    </tr>
    <tr>
      <td height="34">&nbsp;</td>
      <td><input type="submit" name="Submit" value="Ingresar"></td>
    </tr>
  </table>
  <br>
</form>
</body>
</html>
